package com.ych.modules.cms.authorInfc.advertise.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.ych.modules.base.dbEntity.TSysAdvertiseEntity;
import com.ych.modules.cms.authorInfc.advertise.entity.AddAdvertiseEntity;
import com.ych.modules.common.entity.query.QueryAdvertiseEntity;
import com.ych.modules.common.service.AdvertiseService;
import com.ych.utils.mysqlPageUtil.PageUtils;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/1/4
 * Time: 15:27
 * Description:
 */
@RestController("cmsAdvertiseController")
@RequestMapping("/cms/auth/advertise")
@Api(tags = {"CMS——广告相关"}, description = "CMS——广告相关")
public class AdvertiseController {
    @Autowired
    private AdvertiseService advertiseService;

    @PostMapping("/addAdvertise")
    @ApiOperation(value = "新增广告", notes = "新增广告")
    public R<String> addAdvertise(@RequestBody AddAdvertiseEntity entity) {
        try {
            TSysAdvertiseEntity advertiseEntity = JSONObject.parseObject(JSONObject.toJSONBytes(entity), TSysAdvertiseEntity.class);
            advertiseService.addAdvertise(advertiseEntity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/modifyAdvertise")
    @ApiOperation(value = "修改广告", notes = "修改广告")
    public R<String> modifyAdvertise(@RequestBody AddAdvertiseEntity entity) {
        try {
            TSysAdvertiseEntity advertiseEntity = JSONObject.parseObject(JSONObject.toJSONBytes(entity), TSysAdvertiseEntity.class);
            advertiseService.modifyAdvertise(advertiseEntity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/listAdvertise")
    @ApiOperation(value = "查询广告列表", notes = "查询广告列表")
    public R<Page<TSysAdvertiseEntity>> listAdvertise(@RequestBody QueryAdvertiseEntity entity) {
        try {
            PageUtils pageUtils = advertiseService.listAdvertise(entity);
            return new R(0, "成功", pageUtils);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }


    @PostMapping("/getAdvertise")
    @ApiOperation(value = "查询广告详情", notes = "查询广告详情")
    public R<TSysAdvertiseEntity> getAdvertise(@RequestBody String adId) {
        try {
            TSysAdvertiseEntity advertiseEntity = advertiseService.getAdvertise(adId);
            return new R(0,"成功",advertiseEntity);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/removeAd")
    @ApiOperation(value = "删除广告", notes = "删除广告")
    public R<String> removeAd(@RequestBody String adId){
        try {
            advertiseService.removeAd(adId);
            return new R(0,"成功",null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }
}
